/**
 * JalokusNewGame.java
 */

package gameEngine;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ActionMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;

/**
 * @author Joe R. Nassimian
 * @author Yannis Mazzer
 */
public class JalokusNewGame extends JDialog {

	// Variables declaration
	/**
	 * Eclipse generated serial number.
	 */
	private static final long serialVersionUID = 4621681149760940127L;
	private JButton okButton;
	private JButton cancelButton;
    private JComboBox gamesList;
    private JComboBox boardShapeList;
    private JComboBox boardDimensionsList;
    private JComboBox numberOfPlayersList;
    private JLabel dialogText;
    private JLabel gamesNameLabel;
    private JLabel boardsShapeLabel;
    private JLabel boardsDimensionLabel;
    private JLabel numberOfPlayersLabel;
    private Engine app; 
    // End of variables declaration
	    
	public JalokusNewGame(Frame parent, Engine _app){
		super(parent);
		this.app = _app;
		
		// Debug message
		System.out.println("Initializing JalokusNewGame: before buildGUIComponets");
		
		buildGUIComponents();
		
		// Debug message
		System.out.println("Initializing JalokusNewGame: before buildGUILayout");
		
		setGUILayout();
		
		// Debug message
		System.out.println("Initializing JalokusNewGame: after buildGUILayout");
	}

	@Action
	public void cancelButton(){
		// Debug message
		System.out.println("JalokusNewGame.cancelButton(): beginning");
		dispose();
		// Debug message
		System.out.println("JalokusNewGame.cancelButton(): Ending");
	}
		
	@Action
	public void okButton(){
		// Debug message
		System.out.println("JalokusNewGame.okButton(): beginning");
		Settings settings = new Settings(
			(String)gamesList.getSelectedItem(),
			(String)boardShapeList.getSelectedItem(),
			(Integer)boardDimensionsList.getSelectedItem(),
			(Integer)numberOfPlayersList.getSelectedItem());

		// Debug message
		System.out.println("JalokusNewGame.okButton():"+settings.toString());
		
		app.setGameSettings(settings);

		dispose();
		// Debug message
		System.out.println("JalokusNewGame.okButton(): Ending");
	}
		
	/**
	 * This method is called from within the constructor to initialize the form.
	 */
	private void buildGUIComponents(){
		// Debug message
		System.out.println("JalokusNewGame.buildGUIComponents(): beginning");
			
		// Debug message
		System.out.println("JalokusNewGame.buildGUIComponents(): Getting resource and action map");
		ResourceMap resourceMap = app.getContext().getResourceMap(JalokusNewGame.class);
        ActionMap actionMap = app.getContext().getActionMap(JalokusNewGame.class, this);
	        
        // Debug message
		System.out.println("JalokusNewGame.buildGUIComponents(): Setting dialog properties");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setName("New Game"); // NOI18N
		setModal(true);
		setTitle(resourceMap.getString("title")); // NOI18N
		setResizable(false);
			
		// Debug message
		System.out.println("JalokusNewGame.buildGUIComponents(): Setting dialog Text");
		dialogText = new JLabel();
		dialogText.setText(resourceMap.getString("dialogText.text")); // NOI18N
		dialogText.setName("dialogText"); // NOI18N
		
		gamesNameLabel = new JLabel();
		gamesNameLabel.setText(resourceMap.getString("gamesNameLabel.text")); // NOI18N
		gamesNameLabel.setName("gamesNameLabel"); // NOI18N
        
		gamesList = new JComboBox();
		gamesList.setName("gamesList"); // NOI18N
		gamesList.setAction(actionMap.get("gamesList")); // NOI18N
		gamesList.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				okButton.setEnabled(false);
				JComboBox cb = (JComboBox)e.getSource();
				String name = (String)cb.getSelectedItem();
				boardShapeList.setModel(new DefaultComboBoxModel(BoardGame.getGamesBoardShape(name)));
				boardShapeList.setSelectedIndex(0);				
			}
		});

		boardsShapeLabel = new JLabel();
		boardsShapeLabel.setText(resourceMap.getString("boardsShapeLabel.text")); // NOI18N
		boardsShapeLabel.setName("boardsShapeLabel"); // NOI18N
	        
		boardShapeList = new JComboBox();
		boardShapeList.setName("boardShapeList"); // NOI18N
		boardShapeList.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				okButton.setEnabled(false);
				JComboBox cb = (JComboBox)e.getSource();
				String name = (String)gamesList.getSelectedItem();
				String shape = (String)cb.getSelectedItem();
				boardDimensionsList.setModel(new DefaultComboBoxModel(BoardGame.getGamesBoardDim(name, shape)));
				boardDimensionsList.setSelectedIndex(0);
			}
		});

		boardsDimensionLabel = new JLabel();
		boardsDimensionLabel.setText(resourceMap.getString("boardsDimensionLabel.text")); // NOI18N
		boardsDimensionLabel.setName("boardsDimensionLabel"); // NOI18N
        
		boardDimensionsList = new JComboBox();
		boardDimensionsList.setName("boardDimensionsList"); // NOI18N
		boardDimensionsList.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				okButton.setEnabled(false);
				JComboBox cb = (JComboBox)e.getSource();
				String name = (String)gamesList.getSelectedItem();
				String shape = (String)boardShapeList.getSelectedItem();
				Integer dim = (Integer)cb.getSelectedItem();
				numberOfPlayersList.setModel(new DefaultComboBoxModel(BoardGame.getGamesBoardNbPlayers(name, shape, dim.intValue())));
				numberOfPlayersList.setSelectedIndex(0);
			}
		});

		numberOfPlayersLabel = new JLabel();
		numberOfPlayersLabel.setText(resourceMap.getString("numberOfPlayersLabel.text")); // NOI18N
		numberOfPlayersLabel.setName("numberOfPlayersLabel"); // NOI18N
	        
		numberOfPlayersList = new JComboBox();
		numberOfPlayersList.setName("numberOfPlayersList"); // NOI18N
		numberOfPlayersList.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				okButton.setEnabled(true);
			}
		});

		// Debug message
		System.out.println("JalokusNewGame.buildGUIComponents(): Setting buttons");

        okButton = new JButton();
        okButton.setMnemonic('O');
        okButton.setAction(actionMap.get("okButton")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.setEnabled(false);
		getRootPane().setDefaultButton(okButton);

		cancelButton = new JButton();
	    cancelButton.setMnemonic('C');
	    cancelButton.setAction(actionMap.get("cancelButton")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
	        
		gamesList.setModel(new DefaultComboBoxModel(BoardGame.getGamesNameList()));
		gamesList.setSelectedIndex(0);
	        
		// Debug message
		System.out.println("JalokusNewGame.buildGUIComponents(): Ending");
	}


	private void setGUILayout(){
		// Debug message
		System.out.println("JalokusNewGame.buildGUILayout(): Beginning");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        
        /**
         * Setting horizontal layout
         */
		// Debug message
		System.out.println("JalokusNewGame.buildGUILayout(): Setting horizontal layout");
		
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.CENTER)
            .addComponent(dialogText)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                	.addComponent(gamesNameLabel)
                	.addComponent(boardsShapeLabel)
                	.addComponent(boardsDimensionLabel)
                	.addComponent(numberOfPlayersLabel)
                	.addComponent(okButton))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(gamesList)
                	.addComponent(boardShapeList)
                	.addComponent(boardDimensionsList)
                	.addComponent(numberOfPlayersList)
                	.addComponent(cancelButton))));
        
        /**
         * Setting vertical layout
         */
		// Debug message
		System.out.println("JalokusNewGame.buildGUILayout(): Setting vertical layout");
		
        layout.setVerticalGroup(
            layout.createSequentialGroup()
                .addContainerGap()
            	.addComponent(dialogText)
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                	.addComponent(gamesNameLabel)
                    .addComponent(gamesList))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(boardsShapeLabel)
                    .addComponent(boardShapeList))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                	.addComponent(boardsDimensionLabel)
                	.addComponent(boardDimensionsList))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(numberOfPlayersLabel)
                    .addComponent(numberOfPlayersList))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                	.addComponent(okButton)
                	.addComponent(cancelButton))
                	.addContainerGap());
        
		// Debug message
		System.out.println("JalokusNewGame.buildGUILayout(): Before pack()");
        pack();
        
		// Debug message
		System.out.println("JalokusNewGame.buildGUILayout(): Ending");
    }// 

    /*
    * @param args the command line arguments
    */
/*    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JalokusNewGame dialog = new JalokusNewGame(new JFrame());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    */
}